import {ProColumns, ProTable} from "@ant-design/pro-components";
import {GoodsVO, getGoodsPage, removeGoods} from "@/services/GoodsService";
import {Button, Popconfirm, Space} from "antd";
import React from "react";
import {useModel} from "@@/exports";

export interface GoodsProps {
  onSave?: () => void;
  onUpdate?: (record: GoodsVO) => void;
  tableRef?: any;
}


const Goods = (props: GoodsProps) => {
  let {getDictData} = useModel("dict");

  /**
   * 列定义
   */
  const columns: ProColumns<GoodsVO>[] = [
    {
      title: "id",
      dataIndex: "id",
      hideInSearch:true,
      hideInTable: true,
    },
    {
      title: "套系类型",
      dataIndex: "goodsType",
      render: (_, record) => getDictData("goods_type", record.goodsType)?.label
    },
    {
      title: "套系名称",
      dataIndex: "goodsName",
      hideInSearch: true,
    },
    {
      title: "套系描述",
      dataIndex: "goodsDesc",
    },
    {
      title: "价格",
      dataIndex: "price",
      render:(_, record) => `¥${record.price/100}`
    },
    {
      title: "创建时间",
      dataIndex: "createTime",
    },
    {
      title: "更新时间",
      dataIndex: "updateTime",
    },
    {
      title: '操作', render: (_: any, record: GoodsVO) => [
        <Space key="operate">
          <Button type="link" size="small" onClick={() => props.onUpdate && props.onUpdate(record)}>编辑</Button>
          <Popconfirm
            title="确定要删除吗"
            onConfirm={() => {
              removeGoods(record.id).then(() => props.tableRef?.current?.resetFields())
            }}
            okText="确认"
            cancelText="取消"
          >
            <Button type="link" danger size="small">删除</Button>
          </Popconfirm>
        </Space>
      ],
      hideInSearch: true,
    }
  ]

  // 工具栏
  let actions = [
    props?.onSave && <Button key="create" type="primary" onClick={props.onSave}>添加</Button>
  ]

  return <>
    <ProTable
      actionRef={props.tableRef}
      columns={columns}
      request={getGoodsPage}
      toolbar={{
        actions: actions
      }
      }>
    </ProTable>
  </>
}

export default Goods
